home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 February (DVD) / PCWorld_2008-02_DVD.iso / v cisle / PHP / PHP.exe / xampp-win32-1.6.5-installer.exe / phpMyAdmin / libraries / import.lib.php < prev    next >
Encoding:
PHP Script  |  2007-12-20  |  11.0 KB  |  295 lines

  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4.  * Library that provides common import functions that are used by import plugins
  5.  *
  6.  * @version $Id: import.lib.php 10238 2007-04-01 09:26:14Z cybot_tm $
  7.  */
  8.  
  9. /**
  10.  * We need to know something about user
  11.  */
  12. require_once './libraries/check_user_privileges.lib.php';
  13.  
  14. /**
  15.  * We do this check, DROP DATABASE does not need to be confirmed elsewhere
  16.  */
  17. define('PMA_CHK_DROP', 1);
  18.  
  19. /**
  20.  *  Check whether timeout is getting close
  21.  *
  22.  *  @return boolean true if timeout is close
  23.  *  @access public
  24.  */
  25. function PMA_checkTimeout()
  26. {
  27.     global $timestamp, $maximum_time, $timeout_passed;
  28.     if ($maximum_time == 0) {
  29.         return FALSE;
  30.     } elseif ($timeout_passed) {
  31.         return TRUE;
  32.     /* 5 in next row might be too much */
  33.     } elseif ((time() - $timestamp) > ($maximum_time - 5)) {
  34.         $timeout_passed = TRUE;
  35.         return TRUE;
  36.     } else {
  37.         return FALSE;
  38.     }
  39. }
  40.  
  41. /**
  42.  *  Detects what compression filse uses
  43.  *
  44.  *  @param  string filename to check
  45.  *  @return string MIME type of compression, none for none
  46.  *  @access public
  47.  */
  48. function PMA_detectCompression($filepath)
  49. {
  50.     $file = @fopen($filepath, 'rb');
  51.     if (!$file) {
  52.         return FALSE;
  53.     }
  54.     $test = fread($file, 4);
  55.     $len = strlen($test);
  56.     fclose($file);
  57.     if ($len >= 2 && $test[0] == chr(31) && $test[1] == chr(139)) {
  58.         return 'application/gzip';
  59.     }
  60.     if ($len >= 3 && substr($test, 0, 3) == 'BZh') {
  61.         return 'application/bzip2';
  62.     }
  63.     if ($len >= 4 && $test == "PK\003\004") {
  64.         return 'application/zip';
  65.     }
  66.     return 'none';
  67. }
  68.  
  69. /**
  70.  * Runs query inside import buffer. This is needed to allow displaying
  71.  * of last SELECT, SHOW or HANDLER results and similar nice stuff.
  72.  *
  73.  * @uses    $GLOBALS['finished'] read and write
  74.  * @param  string query to run
  75.  * @param  string query to display, this might be commented
  76.  * @param  bool   whether to use control user for queries
  77.  * @access public
  78.  */
  79. function PMA_importRunQuery($sql = '', $full = '', $controluser = false)
  80. {
  81.     global $import_run_buffer, $go_sql, $complete_query, $display_query, $sql_query, $cfg, $my_die, $error, $reload, $timeout_passed, $skip_queries, $executed_queries, $max_sql_len, $read_multiply, $cfg, $sql_query_disabled, $db, $run_query, $is_superuser, $message, $show_error_header;
  82.     $read_multiply = 1;
  83.     if (isset($import_run_buffer)) {
  84.         // Should we skip something?
  85.         if ($skip_queries > 0) {
  86.             $skip_queries--;
  87.         } else {
  88.             if (!empty($import_run_buffer['sql']) && trim($import_run_buffer['sql']) != '') {
  89.                 $max_sql_len = max($max_sql_len, strlen($import_run_buffer['sql']));
  90.                 if (!$sql_query_disabled) {
  91.                     $sql_query .= $import_run_buffer['full'];
  92.                 }
  93.                 if (!$cfg['AllowUserDropDatabase']
  94.                     && !$is_superuser
  95.                     && preg_match('@^[[:space:]]*DROP[[:space:]]+(IF EXISTS[[:space:]]+)?DATABASE @i', $import_run_buffer['sql'])) {
  96.                     $message = $GLOBALS['strNoDropDatabases'];
  97.                     $show_error_header = TRUE;
  98.                     $error = TRUE;
  99.                 } else {
  100.                     $executed_queries++;
  101.                     if ($run_query && $GLOBALS['finished'] && empty($sql) && !$error && (
  102.                             (!empty($import_run_buffer['sql']) && preg_match('/^[\s]*(SELECT|SHOW|HANDLER)/i', $import_run_buffer['sql'])) ||
  103.                             ($executed_queries == 1)
  104.                             )) {
  105.                         $go_sql = TRUE;
  106.                         if (!$sql_query_disabled) {
  107.                             $complete_query = $sql_query;
  108.                             $display_query = $sql_query;
  109.                         } else {
  110.                             $complete_query = '';
  111.                             $display_query = '';
  112.                         }
  113.                         $sql_query = $import_run_buffer['sql'];
  114.                     } elseif ($run_query) {
  115.                         if ($controluser) {
  116.                             $result = PMA_query_as_cu($import_run_buffer['sql']);
  117.                         } else {
  118.                             $result = PMA_DBI_try_query($import_run_buffer['sql']);
  119.                         }
  120.                         $msg = '# ';
  121.                         if ($result === FALSE) { // execution failed
  122.                             if (!isset($my_die)) {
  123.                                 $my_die = array();
  124.                             }
  125.                             $my_die[] = array('sql' => $import_run_buffer['full'], 'error' => PMA_DBI_getError());
  126.  
  127.                             if ($cfg['VerboseMultiSubmit']) {
  128.                                 $msg .= $GLOBALS['strError'];
  129.                             }
  130.  
  131.                             if (!$cfg['IgnoreMultiSubmitErrors']) {
  132.                                 $error = TRUE;
  133.                                 return;
  134.                             }
  135.                         } elseif ($cfg['VerboseMultiSubmit']) {
  136.                             $a_num_rows = (int)@PMA_DBI_num_rows($result);
  137.                             $a_aff_rows = (int)@PMA_DBI_affected_rows();
  138.                             if ($a_num_rows > 0) {
  139.                                 $msg .= $GLOBALS['strRows'] . ': ' . $a_num_rows;
  140.                             } elseif ($a_aff_rows > 0) {
  141.                                 $a_rows =
  142.                                 $msg .= $GLOBALS['strAffectedRows'] . ' ' . $a_aff_rows;
  143.                             } else {
  144.                                 $msg .= $GLOBALS['strEmptyResultSet'];
  145.                             }
  146.                         }
  147.                         if (!$sql_query_disabled) {
  148.                             $sql_query .= $msg . "\n";
  149.                         }
  150.  
  151.                         // If a 'USE <db>' SQL-clause was found and the query succeeded, set our current $db to the new one
  152.                         if ($result != FALSE && preg_match('@^[\s]*USE[[:space:]]*([\S]+)@i', $import_run_buffer['sql'], $match)) {
  153.                             $db = trim($match[1]);
  154.                             $db = trim($db,';'); // for example, USE abc;
  155.                             $reload = TRUE;
  156.                         }
  157.  
  158.                         if ($result != FALSE && preg_match('@^[\s]*(DROP|CREATE)[\s]+(IF EXISTS[[:space:]]+)?(TABLE|DATABASE)[[:space:]]+(.+)@im', $import_run_buffer['sql'])) {
  159.                             $reload = TRUE;
  160.                         }
  161.                     } // end run query
  162.                 } // end if not DROP DATABASE
  163.             } // end non empty query
  164.             elseif (!empty($import_run_buffer['full'])) {
  165.                 if ($go_sql) {
  166.                     $complete_query .= $import_run_buffer['full'];
  167.                     $display_query .= $import_run_buffer['full'];
  168.                 } else {
  169.                     if (!$sql_query_disabled) {
  170.                         $sql_query .= $import_run_buffer['full'];
  171.                     }
  172.                 }
  173.             }
  174.             // check length of query unless we decided to pass it to sql.php
  175.             if (!$go_sql) {
  176.                 if ($cfg['VerboseMultiSubmit'] && ! empty($sql_query)) {
  177.                     if (strlen($sql_query) > 50000 || $executed_queries > 50 || $max_sql_len > 1000) {
  178.                         $sql_query = '';
  179.                         $sql_query_disabled = TRUE;
  180.                     }
  181.                 } else {
  182.                     if (strlen($sql_query) > 10000 || $executed_queries > 10 || $max_sql_len > 500) {
  183.                         $sql_query = '';
  184.                         $sql_query_disabled = TRUE;
  185.                     }
  186.                 }
  187.             }
  188.         } // end do query (no skip)
  189.     } // end buffer exists
  190.  
  191.     // Do we have something to push into buffer?
  192.     if (!empty($sql) || !empty($full)) {
  193.         $import_run_buffer = array('sql' => $sql, 'full' => $full);
  194.     } else {
  195.         unset($GLOBALS['import_run_buffer']);
  196.     }
  197. }
  198.  
  199.  
  200. /**
  201.  * Returns next part of imported file/buffer
  202.  *
  203.  * @uses    $GLOBALS['offset'] read and write
  204.  * @uses    $GLOBALS['import_file'] read only
  205.  * @uses    $GLOBALS['import_text'] read and write
  206.  * @uses    $GLOBALS['finished'] read and write
  207.  * @uses    $GLOBALS['read_limit'] read only
  208.  * @param  integer size of buffer to read (this is maximal size
  209.  *                  function will return)
  210.  * @return string part of file/buffer
  211.  * @access public
  212.  */
  213. function PMA_importGetNextChunk($size = 32768)
  214. {
  215.     global $compression, $import_handle, $charset_conversion, $charset_of_file,
  216.         $charset, $read_multiply;
  217.  
  218.     // Add some progression while reading large amount of data
  219.     if ($read_multiply <= 8) {
  220.         $size *= $read_multiply;
  221.     } else {
  222.         $size *= 8;
  223.     }
  224.     $read_multiply++;
  225.  
  226.     // We can not read too much
  227.     if ($size > $GLOBALS['read_limit']) {
  228.         $size = $GLOBALS['read_limit'];
  229.     }
  230.  
  231.     if (PMA_checkTimeout()) {
  232.         return FALSE;
  233.     }
  234.     if ($GLOBALS['finished']) {
  235.         return TRUE;
  236.     }
  237.  
  238.     if ($GLOBALS['import_file'] == 'none') {
  239.         // Well this is not yet supported and tested, but should return content of textarea
  240.         if (strlen($GLOBALS['import_text']) < $size) {
  241.             $GLOBALS['finished'] = TRUE;
  242.             return $GLOBALS['import_text'];
  243.         } else {
  244.             $r = substr($GLOBALS['import_text'], 0, $size);
  245.             $GLOBALS['offset'] += $size;
  246.             $GLOBALS['import_text'] = substr($GLOBALS['import_text'], $size);
  247.             return $r;
  248.         }
  249.     }
  250.  
  251.     switch ($compression) {
  252.         case 'application/bzip2':
  253.             $result = bzread($import_handle, $size);
  254.             $GLOBALS['finished'] = feof($import_handle);
  255.             break;
  256.         case 'application/gzip':
  257.             $result = gzread($import_handle, $size);
  258.             $GLOBALS['finished'] = feof($import_handle);
  259.             break;
  260.         case 'application/zip':
  261.             $result = substr($GLOBALS['import_text'], 0, $size);
  262.             $GLOBALS['import_text'] = substr($GLOBALS['import_text'], $size);
  263.             $GLOBALS['finished'] = empty($GLOBALS['import_text']);
  264.             break;
  265.         case 'none':
  266.             $result = fread($import_handle, $size);
  267.             $GLOBALS['finished'] = feof($import_handle);
  268.             break;
  269.     }
  270.     $GLOBALS['offset'] += $size;
  271.  
  272.     if ($charset_conversion) {
  273.         return PMA_convert_string($charset_of_file, $charset, $result);
  274.     } else {
  275.         /**
  276.          * Skip possible byte order marks (I do not think we need more
  277.          * charsets, but feel free to add more, you can use wikipedia for
  278.          * reference: <http://en.wikipedia.org/wiki/Byte_Order_Mark>)
  279.          *
  280.          * @todo BOM could be used for charset autodetection
  281.          */
  282.         if ($GLOBALS['offset'] == $size) {
  283.             // UTF-8
  284.             if (strncmp($result, "\xEF\xBB\xBF", 3) == 0) {
  285.                 $result = substr($result, 3);
  286.             // UTF-16 BE, LE
  287.             } elseif (strncmp($result, "\xFE\xFF", 2) == 0 || strncmp($result, "\xFF\xFE", 2) == 0) {
  288.                 $result = substr($result, 2);
  289.             }
  290.         }
  291.         return $result;
  292.     }
  293. }
  294. ?>
  295.